package handler

import (
	"context"
	"fmt"
	"user-server/global"
	"user-server/model"
	__ "user-server/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) AddGroup(_ context.Context, in *__.AddGroupRequest) (*__.AddGroupResponse, error) {
	group := model.Groups{
		Title:   in.Title,
		Price:   float64((in.Price)),
		Address: in.Address,
		Name:    in.Name,
		Phone:   in.Phone,
	}
	global.DB.Debug().Create(&group)
	return &__.AddGroupResponse{}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ListGroup(_ context.Context, in *__.ListGroupRequest) (*__.ListGroupResponse, error) {
	var ListGroup []model.Groups
	global.DB.Debug().Order("price desc").Find(&ListGroup)
	var list []*__.ListGroup
	for _, v := range ListGroup {
		list = append(list, &__.ListGroup{
			Id:      int64(v.Id),
			Title:   v.Title,
			Price:   float32(v.Price),
			Address: v.Address,
			Name:    v.Name,
			Phone:   v.Phone,
		})
	}
	return &__.ListGroupResponse{List: list}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) InfoGroup(_ context.Context, in *__.InfoGroupRequest) (*__.InfoGroupResponse, error) {
	var info model.Groups
	global.DB.Debug().Find(&info)
	if info.Id == 0 {
		return nil, fmt.Errorf("订单不存在")
	}
	infos := model.Groups{
		Id: uint(in.Id),
	}
	global.DB.Debug().Find(&infos)
	return &__.InfoGroupResponse{
		Title:   infos.Title,
		Price:   float32(infos.Price),
		Address: infos.Address,
		Name:    infos.Name,
		Phone:   infos.Phone,
	}, nil
}
